﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;

namespace Chapter07
{
    public class Code01
    {
        public static void Begin()
        {
            {
                Console.WriteLine("MyFunc() Function about to be called.");
                MyFunc("Do something.");
                Console.WriteLine("MyFunc() execution completed.");
                Console.ReadLine();
            }
            {
                Debug.WriteLine("Added i to i", "MyFunc");
                Console.ReadLine();
            }
            {
                Debug.Assert(true);
                Trace.Assert(true);
                Console.ReadLine();
            }
        }

        public static void BeginEx01()
        {
            int[] testArray = { 4, 7, 4, 2, 7, 3, 7, 8, 3, 9, 1, 9 };
            int[] maxValIndices;
            int maxVal = Maxima(testArray, out maxValIndices);
            foreach (int index in maxValIndices)
            {
                Console.WriteLine(index);
            }
            Console.ReadLine();
        }

        private static void MyFunc(string info)
        {
            Console.WriteLine(info);
        }

        private static int Maxima(int[] intergers, out int[] indices)
        {
            //Debug.WriteLine("Maximun value search started.");
            indices = new int[1];
            int maxVal = intergers[0];
            indices[0] = 0;
            int count = 1;
            //Debug.WriteLine(string.Format("Maximun value initialized to {0}, at element index 0.", maxVal));
            for (int i = 1; i < intergers.Length; i++)
            {
                //Debug.WriteLine(string.Format("New looking at element at index {0}.", i));
                if (intergers[i] > maxVal)
                {
                    maxVal = intergers[i];
                    count = 1;
                    indices = new int[1];
                    indices[0] = i;
                    //Debug.WriteLine(string.Format("New maximun found. New value is {0}, at element index {1}.", maxVal, i));
                }
                else
                {
                    if (intergers[i] == maxVal)
                    {
                        count++;
                        int[] oldIndiecs = indices;
                        indices = new int[count];
                        oldIndiecs.CopyTo(indices, 0);
                        indices[count - 1] = i;
                        //Debug.WriteLine(string.Format("Duplicate maximun found at element index {0}.", i));
                    }
                }
            }
            Trace.WriteLine(string.Format("Maximun value {0} found, with {1} ocurrences.", maxVal, count));
            //Debug.WriteLine("Maximun value search compleled.");
            return maxVal;
        }
    }
}
